//
//  CYSingleTicketViewController.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/12.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYSingleTicketViewController.h"
#import "CYSingleTicketCell.h"
#import "CYTicketDetailViewController.h"
#import "CYLotteryWinModel.h"
@interface CYSingleTicketViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) CYLotteryWinModel *model;
@property (nonatomic, copy) NSString *singleTicketPlistName;
@property (nonatomic, assign) int page;//每次请求的页数
@property (nonatomic, assign) int rows;//每页的行数

@end

@implementation CYSingleTicketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    _rows = ceil((SCREEN_HEIGHT - NAVIBAR_HEIGHT - STATUS_HEIGHT - 100) / 60) + 1;
    _singleTicketPlistName = [NSString stringWithFormat:@"%@_history.plist",self.navigationItem.title];
    // Do any additional setup after loading the view.
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.mj_header = [CYRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshDown)];//下拉刷新
        _tableView.mj_footer = [CYRefreshFooder footerWithRefreshingTarget:self refreshingAction:@selector(refreshUp)];//上拉加载
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).with.offset(NAVIBAR_HEIGHT + STATUS_HEIGHT);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - NAVIBAR_HEIGHT - STATUS_HEIGHT));
            make.centerX.equalTo(self.view);
        }];
    }
    return _tableView;
}

- (void)setModel:(CYLotteryWinModel *)model{
    if (_model != model) {
        if (_model.lotteries.count < 1)
            _model = model;
        else if(_model.lotteries.count/_rows < model.total_page && _page > 1)
            [_model.lotteries addObjectsFromArray:model.lotteries];
        else if (_page == 1)
            [_model.lotteries replaceObjectsInRange:NSMakeRange(0, _rows) withObjectsFromArray:model.lotteries];
        if (!_tableView)
            self.tableView.hidden = NO;
        else
            [self.tableView reloadData];
    }
}

//下啦刷新
- (void)refreshDown{
    _page = 1;
    [self https];
}

//上啦加载
- (void)refreshUp{
    _page = (int)_model.lotteries.count / _rows;
    _page++;
    [self https];
}

- (void)https{
    self.task = [CYHttps POST:API_result_detail parameters:@{@"id":@(_lotteryNoticeModel.ID),@"page":@(_page), @"number":@(_rows)} showHUD:YES progress:nil success:^(id data) {
        if (_tableView){
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
        }
        self.model = [CYLotteryWinModel mj_objectWithKeyValues:data];
    } failure:nil];
    
}


#pragma - mark - UITableViewDataSource method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _model.lotteries.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = @"CYSingleTicketCell";
    CYSingleTicketCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
        cell = [[CYSingleTicketCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.isFirst = (!indexPath.row)?YES:NO;
    cell.lotteryName = _model.name;
    cell.winNumModel = _model.lotteries[indexPath.row];
    return cell;
    
}
#pragma - mark - UITableViewDelegate method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return (!indexPath.row)?100.0:60.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CYWinNumberModel *subModel = _model.lotteries[indexPath.row];
    CYTicketDetailViewController *ticket_detail = [[CYTicketDetailViewController alloc] init];
    ticket_detail.para = @{@"id":@(_model.ID),@"number":subModel.actionNo};//id 彩种id, number期号
    [self.navigationController pushViewController:ticket_detail animated:NO];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self https];
    
}

- (void)setupNavigationBarTintColor:(UIColor *)color{
    [super setupNavigationBarTintColor:color];
    self.navigationController.navigationItem.title = _lotteryNoticeModel.name;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
